package com.example.k8s.hello.bean.exam.form;

import com.example.k8s.hello.bean.exam.constant.EdConstant;
import com.example.k8s.hello.bean.exam.entity.EdExamDetailEntity;
import com.example.k8s.hello.bean.exam.entity.EdExamEntity;
import com.example.k8s.hello.common.bean.LoginUser;
import com.example.k8s.hello.common.utils.CopyUtils;
import com.example.k8s.hello.common.utils.StpExtendUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Schema(description = "考试保存表单")
public class EdExamSaveForm {
    @NotNull
    @Schema(description = "考试科目")
    private EdConstant.SubjectType subject;

    @NotNull
    @Schema(description = "考试场景")
    private EdConstant.SceneType scene;

    @NotNull
    @Schema(description = "血量")
    private BigDecimal hp;

    @NotNull
    @Schema(description = "考试项目结果列表")
    private List<EdExamDetailForm> details;

    @Data
    @Schema(description = "考试项目结果")
    public static class EdExamDetailForm {
        @NotEmpty
        @Schema(description = "考试项目")
        private EdConstant.ExamItem item;

        @NotEmpty
        @Schema(description = "考试是否通过")
        private Boolean itemPass;
    }

    public EdExamEntity buildExam() {
        EdExamEntity examEntity = CopyUtils.copy(this, EdExamEntity.class);

        LoginUser loginUser = StpExtendUtil.getLoginUser();

        return examEntity.setUserId(loginUser.getUserId())
                .setUsername(loginUser.getUsername())
                .setNickname(loginUser.getNickname())
                .setPass(examEntity.getHp().compareTo(new BigDecimal(0)) > 0)
                .setScore(getScore());
    }

    public List<EdExamDetailEntity> buildDetails(Long examId) {
        return details.stream().map(detail ->
                new EdExamDetailEntity()
                        .setExamId(examId)
                        .setUserId(StpExtendUtil.getUserId())
                        .setScene(scene)
                        .setSubject(subject)
                        .setItem(detail.item)
                        .setItemPass(detail.itemPass))
                .collect(Collectors.toList());
    }

    /**
     * 计算成绩(详见算分逻辑.md)
     * @return
     */
    private Integer getScore() {
        if (hp.compareTo(new BigDecimal(0)) <= 0) {
            return 0;
        }

        BigDecimal score = new BigDecimal(0);

        int totalSize = details.size() + 1;

        // 每项的成绩
        BigDecimal itemScore = new BigDecimal(100).divide(new BigDecimal(totalSize), 2, RoundingMode.HALF_UP);

        // 通过的项目数
        long passItems = details.stream().filter(detail -> detail.getItemPass()).count();

        score = score.add(itemScore.multiply(new BigDecimal(passItems)));

        // 生命值项的成绩
        BigDecimal hpRate = new BigDecimal(1);
        if (hp.compareTo(new BigDecimal(EdConstant.FULL_MARKS_HP)) < 0) {
            hpRate = hp.divide(new BigDecimal(EdConstant.FULL_MARKS_HP), 2, RoundingMode.HALF_UP);
        }
        score = score.add(itemScore.multiply(hpRate));

        return score.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    }
}
